package com.ktgj.common.core.utils.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class ZipHelper
{
	private final static Logger LOGGER = LoggerFactory.getLogger(ZipHelper.class);

	public static void extractZip(String zipFileName, String targetDirectory)
	{
		try
		{
			FileInputStream fis = new FileInputStream(zipFileName);
			@SuppressWarnings("resource") ZipInputStream zipIn = new ZipInputStream(fis);
			ZipEntry zipEntry = null;
			while ((zipEntry = zipIn.getNextEntry()) != null)
			{
				if (zipEntry.isDirectory())
				{
					String dir = PathHelper.combine(targetDirectory, zipEntry.getName());
					dir = dir + File.separator;
					File d = new File(dir);
					if (!d.exists())
					{
						d.mkdirs();
					}

				}
				else
				{
					String fileName = PathHelper.combine(targetDirectory, zipEntry.getName());
					File file = new File(fileName);
					file.createNewFile();
					FileOutputStream stream = new FileOutputStream(file);

					int max = 1024;
					long length = zipEntry.getSize();
					long left = length;
					while (left > 0)
					{
						int toReadSize = (int) (left > max ? max : left);
						byte[] buffer = new byte[toReadSize];
						int count = zipIn.read(buffer);
						stream.write(buffer, 0, count);
						left -= count;
					}
					stream.flush();
					stream.close();
				}

			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			LOGGER.error(e.getMessage());
		}
	}

	public static void compress(String warDir, String fileName)
	{
		ZipOutputStream zipOutputStream = null;
		FileOutputStream stream = null;
		try
		{
			File file = new File(warDir);
			if (!file.exists()) return;

			stream = new FileOutputStream(fileName);
			zipOutputStream = new ZipOutputStream(stream);
			if (file.isDirectory())
			{
				compressDirectory(zipOutputStream, file, "");
			}
			else
			{
				compressFile(zipOutputStream, file, "");
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (zipOutputStream != null)
			{
				try
				{
					zipOutputStream.flush();
					zipOutputStream.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}

			if (stream != null)
			{
				try
				{
					stream.flush();
					stream.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}

	static void compressDirectory(ZipOutputStream zipOutputStream, File direcotry, String path)
	{
		File[] files = direcotry.listFiles();
		String dirEntry = path + direcotry.getName() + "/";
		ZipEntry entry = new ZipEntry(dirEntry);
		try
		{
			zipOutputStream.putNextEntry(entry);
			if (files != null)
			{
				for (File file : files)
				{
					if (file.isDirectory())
					{
						compressDirectory(zipOutputStream, file, dirEntry);
					}
					else
					{
						compressFile(zipOutputStream, file, dirEntry);
					}
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	static void compressFile(ZipOutputStream zipOutputStream, File file, String path)
	{
		try
		{
			byte[] buffer = new byte[1024];
			FileInputStream inputStream = new FileInputStream(file);
			zipOutputStream.putNextEntry(new ZipEntry(path + file.getName()));

			int len;
			while ((len = inputStream.read(buffer, 0, 1024)) != -1)
			{
				zipOutputStream.write(buffer, 0, len);
			}

			inputStream.close();
			zipOutputStream.closeEntry();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void compressZip(String zipFileName, String targetDirectory)
	{
		ZipOutputStream zipOutputStream = null;
		try
		{
			File outFile = new File(targetDirectory + "\\project.zip");
			zipOutputStream = new ZipOutputStream(new FileOutputStream(outFile));
			File file = new File(zipFileName);
			if (file.isDirectory())
			{
				File[] files = file.listFiles();
				if (files != null)
				{
					for (int i = 0; i < files.length; i++)
					{
						compressZip(files[i].getPath(), targetDirectory);
					}
				}
			}
			else
			{
				byte[] buffer = new byte[1024];
				FileInputStream inputStream = new FileInputStream(file);
				zipOutputStream.putNextEntry(new ZipEntry(outFile.getName()));
				int len;
				while ((len = inputStream.read(buffer, 0, 1024)) != -1)
				{
					zipOutputStream.write(buffer, 0, len);
				}

				inputStream.close();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (zipOutputStream != null)
			{
				try
				{
					zipOutputStream.flush();
					zipOutputStream.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}

}
